<?php
class ProductAction extends BaseAction {

    //我的产品列表
    public function productList() {
        import("ORG.Util.Page"); //引入分页类
        $product_model = M('mer_product');

        $where['mer_product.mer_userid'] = array('eq', $this->user_info['userid']);
        $where['mer_product.status'] = array('gt', '0');
        $count = $product_model->where($where)->count(); // 查询满足要求的总记录数
        $page = new Page($count, 100); // 实例化分页类
        $product_data = $product_model->field("mer_product.*,mer_product_connection.productid")->where($where)
                        ->join('mer_product_connection on mer_product_connection.mer_productid = mer_product.id')
                        ->order("mer_product.id desc")->limit($page->firstRow . "," . $page->listRows)->select();
        $page_show = $page->show();
		$this->assign('cur_sidebar','productList');
        $this->assign('product_data', $product_data); //产品数据
        $this->assign('page', $page_show);
        $this->display();
    }

    //上传产品界面
    public function uploadProductHtml() {
        $mer_productid = $_REQUEST['mer_productid'];
        $lanmu_title_ch = "上传产品";
        $lanmu_title_en = "Upload-Product";
        $lanmu_url = U ( 'Product/uploadProductHtml');
        if (!empty($mer_productid)) {
            $lanmu_title_ch = '编辑产品';
            $lanmu_title_en = "Edit-Product";
            $lanmu_url = U ( 'Product/uploadProductHtml/mer_productid/' . $mer_productid);
            $product_model = M('mer_product');
            $product_data = $product_model->where("id=" . $mer_productid . " and mer_userid = " . $this->user_info['userid'])->find();
            if (empty($product_data)) {
                echo "<script>alert('产品不存在');history.go(-1);</script>";
            }
            $this->assign('product_data', $product_data);
        }
		$this->assign('cur_sidebar','upload');
        $this->assign('lanmu_title_ch', $lanmu_title_ch);
        $this->assign('lanmu_title_en', $lanmu_title_en);
        $this->assign('lanmu_url', $lanmu_url);
        $this->display();
	   
    }

    //保存上传产品信息
    public function saveProductInfo() {
        $product_model = M('mer_product');
        $data['title'] = trim($_REQUEST['mer_title']);
        $data['price'] = trim($_REQUEST['mer_price']);
        $data['description'] = $_REQUEST['mer_description'];
        $data['mer_userid'] = $this->user_info['userid'];
        $mer_productid = $_REQUEST['mer_productid_inp'];
        $log_data = array();
        if (!empty($mer_productid)) {
            $product_model->where("id=" . $mer_productid . " and mer_userid=" . $this->user_info['userid'])->data($data)->save();
            $log_data['type'] = 'edit_product';
            $log_data['content'] = '修改产品:' . $mer_productid;
            //header("location:/Product/productList/classname/mp");
			header("Location:".U ( 'Product/productList'));
        } else {
            $id = $product_model->data($data)->add();
			if(!empty($id)){
				//$this->success("保存成功");
				//header("Location:".U ( 'Product/uploadProductHtml'));
				//redirect(U ( 'Product/uploadProductHtml'));
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script>alert('上传成功');history.go(-2);</script>"; // history.back(-1) history.go(-2) window.location.reload()
				
				// header("Location:".U ( 'Product/uploadProductHtml'));
			   //echo "<script>location.replace(document.referrer);</script>";
			}
			$log_data['type'] = 'add_product';
            $log_data['content'] = '添加新产品:' . $id;
        }
        $this->saveLogData($log_data); //记录日志
    }


    //删除产品
    public function delProduct() {
        $product_model = M('mer_product');
        $product_connection_model = M('mer_product_connection');
        $id = $_REQUEST['mer_productid'];
        $product_model->where("id=" . $id . " and mer_userid=" . $this->user_info['userid'])->setField('status', 0);
        $product_connection_model->where("mer_productid=" . $id . " and mer_userid=" . $this->user_info['userid'])->setField('status', 0);
	
        //添加日志
        $log_data['type'] = 'del_product';
        $log_data['content'] = '删除产品:' . $id;
        $this->saveLogData($log_data);
        echo "<script>location.replace(document.referrer);</script>";
    }


    //订单报价列表
    public function orderQuoteHtml() {
        import("ORG.Util.Page"); //引入分页类
        $where = array();
        $list = array();
		$data = array();
        $connection =  M('mer_product_connection');
       	$where['smc.mer_userid'] = $this->user_info['userid'];
		$sum = $connection->field('smc.id,smq.id as qid,smq.mer_userid,eog.product_name,smq.price,smq.nums,smq.addtime,smq.status,smc.productid,smc.mer_productid,sum(eog.number) as product_nums,eog.order_id')->join('as smc left join '. $this->erp .'.order_goods eog on eog.product_id = smc.productid left join mer_product_quote smq on smq.mer_productid = smc.mer_productid left join '. $this->erp .'.order as o on eog.order_id = o.id')->where($where)->group('smc.productid')->order('smq.addtime desc')->select();
		$count = count($sum);
		$page = new Page($count, 20); // 实例化分页类
		$page_show = $page->show();
		$list = $connection->field('smc.id,smq.id as qid,smq.mer_userid,eog.product_name,smq.price,smq.nums,smq.addtime,smq.status,smc.productid,smc.mer_productid,sum(eog.number) as product_nums,eog.order_id')->join('as smc left join '. $this->erp .'.order_goods eog on eog.product_id = smc.productid left join mer_product_quote smq on smq.mer_productid = smc.mer_productid left join '. $this->erp .'.order as o on eog.order_id = o.id')->limit($page->firstRow . "," . $page->listRows)->where($where)->group('smc.productid')->order('smq.addtime desc')->select();
		foreach($list as $row){
			if(!empty($row['product_name'])){
				$new_data[$i]['id'] = $row['id'];
				$new_data[$i]['only_id'] = $row['order_id'] . '_' . $row['mer_productid'];
				$new_data[$i]['productid'] = $row['productid'];
				$new_data[$i]['mer_productid'] = $row['mer_productid'];
				$new_data[$i]['mer_userid'] = $row['mer_userid'];
				$new_data[$i]['title'] = $row['product_name'];
				$new_data[$i]['order_id'] = $row['order_id'];
				$new_data[$i]['product_nums'] = $row['product_nums'];
				$key_wd = $row['order_id'] . '_' . $row['mer_productid'] . '_' . $row['mer_userid'];
				$quotedata = $quote_data[$key_wd];
				$new_data[$i]['quote_id'] = $row['qid'];
				$new_data[$i]['bao_jia'] = $row['price'];
				$new_data[$i]['quote_time'] = $row['addtime'];
				$new_data[$i]['status'] = $row['status'];
				$i++;
			}
		}
		unset($list);
		$this->assign('cur_sidebar','order');
        $this->assign('product_data', $new_data); //产品数据
        $this->assign('page', $page_show);
        
        $this->display();
    }

    //订单报价保存
    public function saveProductPriceData() {
    	
        $mer_product_quote_model = M('mer_product_quote');
		$mer_product_quote_model_log = M('mer_product_quote_log');
		$list = array();
		$list['oldprice']=$_REQUEST['jia'];
		$list['order_id'] = $_REQUEST['order_id'];
		$list['productid'] = $_REQUEST['productid'];
		$list['mer_productid'] = $_REQUEST['mer_productid'];
		$list['price'] = $_REQUEST['price'];
		$list['mer_userid'] = $this->user_info['userid'];
		$list['addtime'] = date('Y-m-d H:i:s');
        $data['order_id'] = $_REQUEST['order_id'];
        $data['productid'] = $_REQUEST['productid'];
        $data['mer_productid'] = $_REQUEST['mer_productid'];
        $data['price'] = $_REQUEST['price'];
        $data['nums'] = $_REQUEST['product_nums'];
        $data['mer_userid'] = $this->user_info['userid'];
        $data['addtime'] = date('Y-m-d H:i:s');
        $product_quoteid = $_REQUEST['product_quoteid'];
        if (!empty($product_quoteid)) {
        	$mer_product_quote_model_log->add($list);
            $mer_product_quote_data = $mer_product_quote_model->where("id=" . $product_quoteid)->find();
            $array = array('price' => $_REQUEST['price'], 'addtime' => date('Y-m-d H:i:s'));
            $mer_product_quote_model->where("id=" . $product_quoteid . " and mer_userid=" . $this->user_info['userid'])->setField($array);
            $mer_history_price_model = M('mer_history_price');
            $history_data['product_quoteid'] = $product_quoteid;
            $history_data['order_id'] = $mer_product_quote_data['order_id'];
            $history_data['productid'] = $mer_product_quote_data['productid'];
            $history_data['mer_productid'] = $mer_product_quote_data['mer_productid'];
            $history_data['price'] = $mer_product_quote_data['price'];
            $history_data['nums'] = $mer_product_quote_data['nums'];
            $history_data['mer_userid'] = $mer_product_quote_data['mer_userid'];
            $history_data['addtime'] = $mer_product_quote_data['addtime'];
            $row = $mer_history_price_model->data($history_data)->add();
			if($row>0){
				$arr=array('row'=>$row);
				echo json_encode($arr);
			}else{
				$arr=array('row'=>0);
				exit(json_encode($arr));
			}
        } else {
        	$mer_product_quote_model_log->add($list);
            $row = $mer_product_quote_model->data($data)->add();
			if($row>0){
				$arr=array('row'=>$row);
				echo json_encode($arr);
			}else{
				$arr=array('row'=>0);
				exit(json_encode($arr));
			}
        }
    }

    //打包上传产品
    public function packProductHtml() {
        import("ORG.Util.Page"); //引入分页类
        $pack_product_model = M("mer_pack_product");
        $where['mer_userid'] = array('eq', $this->user_info['userid']);
        $where['status'] = array('gt', 0);
        $count = $pack_product_model->where($where)->count(); // 查询满足要求的总记录数
        $page = new Page($count, 100); // 实例化分页类
        $pack_product_data = $pack_product_model->where($where)->order("id desc")->limit($page->firstRow . "," . $page->listRows)->select();
        $page_show = $page->show();
		$this->assign('cur_sidebar','pack');
        $this->assign('pack_product_data', $pack_product_data); //产品数据
        $this->assign('page', $page_show);
        $this->display();
    }

    //保存上传的打包产品
    public function savePackProduct() {
        $file_info = $_FILES['file_input'];
        $file_name = $file_info['name']; //文件名
        $file_tmp_name = $file_info['tmp_name']; //路劲
        $file_types = explode(".", $file_name);
        $type = array("zip", "rar","jpg");
        $file_type = strtolower($file_types [count($file_types) - 1]); //文件格式
        //判断上传格式
        if (!in_array($file_type, $type)) {
            echo "<script>alert('文件格式错误');history.go(-1)</script>";
            die();
        }
        /* 设置上传路径(分站点) */
        $path = $this->user_info['userid'];
        $savePath = realpath("./") . '/Data/PackProduct/' . $path;
        if (!is_file($savePath)) {
            mkdir($savePath);
        }
        $path.='/' . date('Y-m');
        $savePath2 = realpath("./") . '/Data/PackProduct/' . $path;
        if (!is_file($savePath2)) {
            mkdir($savePath2);
        }
        $data = array();
        /* 以加时间命名 */
        $new_name = $this->user_info['userid'] . "_" . date('Ymdhis') . "_" . rand(10, 99) . '.' . $file_type;
        $data['name'] = $new_name;
        $data['path'] = '/' . $path . '/' . $new_name;
        $data['old_name'] = $file_name;
        $data['mer_userid'] = $this->user_info['userid'];
        /* 是否上传成功 */
        if (copy($file_tmp_name, $savePath2 . "/" . $new_name)) {
            $pack_product_model = M("mer_pack_product");
            $id = $pack_product_model->data($data)->add();
            //记录日志
            $log_data['type'] = 'add_pack_product';
            $log_data['content'] = '添加产品打包文件:' . $id;
            $this->saveLogData($log_data);
            echo "<script>location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('上传失败');history.go(-1)</script>";
        }
    }

    //删除打包产品
    public function delPackProduct() {
        $pack_product_model = M("mer_pack_product");
        $id = $_REQUEST['id'];
        $pack_product_model->where("id=" . $id . " and mer_userid=" . $this->user_info['userid'])->setField('status', 0);
        //添加日志
        $log_data['type'] = 'del_pack_product';
        $log_data['content'] = '删除打包产品:' . $id;
        $this->saveLogData($log_data);
        echo "<script>location.replace(document.referrer);</script>";
    }

}

?>